package cn.com.generaldata.os.service.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.paging.Event;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.paging.Page;
import cn.com.generaldata.os.paging.Result;
import cn.com.generaldata.os.pojo.Area;
import cn.com.generaldata.os.pojo.AreaPerform;

@Component
public class AreaService {

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<Area> areaDAO;

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<AreaPerform> areaPerformDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#createArea(cn.com.generaldata.os.pojo.Area)
	 */

	public String createArea(Area area) {
		if (area != null) {
			if (StringUtils.isNotBlank(area.getAreaName())) {
				Area oldArea = checkAreaName(area.getAreaName());
				if (oldArea != null) {
					// throw new CommonException(
					// ExceptionConstant.CODE_DUPLICATE_ROLE_NAME,
					// ExceptionConstant.CAUSE_CREATE_ROLE);
				}
			}

			areaDAO.save(area);
			return area.getId();
		}
		return null;
	}

	//查询全部下级

	public List<Area> findChildren(String areaId, List<Area> list) {
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.NOT_NULL, "areaCode", null));
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "parentId", areaId));
		List<Area> areas = areaDAO.find(Area.class, filter, null);
		if (areas != null && areas.size() > 0) {
			list.addAll(areas);
			for (Area area : areas) {
				findChildren(area.getId(), list);
			}
		}
		return list;
	}

	//只查询直属下级

	public List<AreaPerform> getChildren(String areaId, List<AreaPerform> list) {
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.NOT_NULL, "areaCode", null));
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "parentId", areaId));
		List<AreaPerform> areas = areaPerformDAO.find(AreaPerform.class, filter, null);
		if (areas != null && areas.size() > 0) {
			list.addAll(areas);
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#modifyArea(cn.com.generaldata.os.pojo.Area)
	 */

	public Area modifyArea(Area area) {
		if (area != null) {
			areaDAO.update(area);
			return area;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#queryAreaById(java.lang.String)
	 */

	public Area queryAreaById(String id) {
		if (id != null) {
			return areaDAO.get(Area.class, id);
		}
		return null;
	}

	public Result queryAreasHql(String hql, Page page, Object... paras) {
		// List<User> users = userDAO.find(User.class, filter, page);
		List<Area> findByHql = areaDAO.findByHql(hql, paras);
		List<Area> users = new ArrayList<Area>();
		if (page == null) {

			users = areaDAO.findEntitiesByHql(hql, 0, 20, paras);
			return new Result(1, 1, findByHql.size(), users, new ArrayList<Event>());
		} else {
			users = areaDAO.findEntitiesByHql(hql, page.getFirstResult(), page.getPageSize(), paras);
		}
		return new Result(page.getPageNumber(), page.getPageSize(), findByHql.size(), users, new ArrayList<Event>());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#queryAreas(cn.com.generaldata.os.paging.Filter,
	 * cn.com.generaldata.os.paging.Page)
	 */

	public Result queryAreas(Filter filter, Page page) {
		List<Area> areas = areaDAO.find(Area.class, filter, page);
		int count = areaDAO.count(Area.class, filter);

		if (page == null) {
			return new Result(1, 1, count, areas, new ArrayList<Event>());
		}
		return new Result(page.getPageNumber(), page.getPageSize(), count, areas, new ArrayList<Event>());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.generaldata.os.service.impl.AreaServiceInterface#removeAreas(java.lang.String[])
	 */
	public void removeAreas(String[] ids) {
		if (ids != null && ids.length > 0) {

			Filter filter = new Filter();
			filter.addRule(new FilterRule(FilterConstant.COLLECTION, "id", ids));

			List<Area> areas = areaDAO.find(Area.class, filter, null);
			areaDAO.delete(areas);
		}
	}

	private Area checkAreaName(String name) {
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaName", name));

		List<Area> areas = areaDAO.find(Area.class, filter, null);
		if (areas.size() > 0) {
			return areas.get(0);
		}
		return null;
	}

}
